<template>
  <div class="view_container">
    <el-card class="view_list">
      <i class="el-icon-tickets"></i>
      <span class="view_header_title">数据列表</span>
      <el-button
        class="fr ml20"
        size="small"
        type="primary"
        v-permission="'addMenu'"
        @click="edit()"
        >添加</el-button
      >
    </el-card>
    <el-table :data="tableData" border style="width: 100%" size="small">
      <el-table-column
        align="center"
        header-align="center"
        prop="id"
        label="编号"
        width="50"
      >
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        prop="title"
        label="菜单名称"
      >
      </el-table-column>
      <el-table-column align="center" header-align="center" label="菜单级数">
        <template slot-scope="scope">{{
          ["一级", "二级"][scope.row.level]
        }}</template>
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        prop="name"
        label="前端名称"
      >
      </el-table-column>
      <el-table-column align="center" header-align="center" label="前端图标">
        <template slot-scope="scope">
          <i :class="['iconfont', scope.row.icon, 'colorb']"></i>
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="是否显示">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.hidden"
            :active-value="0"
            :inactive-value="1"
            @change="statusChange(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        prop="sort"
        label="排序"
      >
      </el-table-column>
      <el-table-column align="center" header-align="center" label="设置">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            :disabled="scope.row.level | disableNextLevel"
            @click="handleShowNextLevel(scope.row)"
            >查看下级
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="操作"
        width="160"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            v-permission="'editMenu'"
            size="small"
            @click="edit(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="del(scope.row)"
            v-permission="'delMenu'"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pag_content"
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getMenuLists, updateMenuHidden, delRoleLists } from "@/request/api";
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      parentId: 0,
    };
  },
  components: {},
  computed: {
    permission() {
      return this.$store.getters.permission;
    },
  },
  watch: {
    $route() {
      this.parentId = this.$route.query.pid || 0;
      this.fetchData();
    },
  },
  filters: {
    disableNextLevel(level) {
      return !!level;
    },
  },
  methods: {
    handleShowNextLevel(row) {
      this.$router.push("/ums/menu?pid=" + row.id);
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.fetchData();
    },
    handleCurrentChange(page) {
      this.currentPage = page;
      this.fetchData();
    },
    fetchData() {
      getMenuLists(this.parentId, {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
      }).then((res) => {
        if (res.code === 200) {
          this.tableData = res.data.list;
          this.total = res.data.total;
        }
      });
    },
    statusChange(row) {
      //   console.log(row);
      updateMenuHidden(row.id, {
        hidden: row.hidden,
      }).then((res) => {
        if (res.code == 200) {
          this.$message.success("状态修改成功");
        }
      });
    },

    edit(row) {
      if (row && row.id) {
        this.$router.push({
          path: "/ums/addMenu",
          query: {
            id: row.id,
          },
        });
      } else {
        this.$router.push("/ums/addMenu");
      }
    },
    del(row) {
      this.$confirm(`此操作将删除当前用户, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delRoleLists({ ids: row.id }).then((res) => {
            if (res.code == 200) {
              this.fetchData();
              this.$message.success(res.message);
            }
          });
        })
        .catch(() => {});
    },
  },
  created() {
    this.parentId = this.$route.query.pid || 0;
    this.fetchData();
  },
};
</script>

<style lang="less" scoped>
.view_header_title {
  padding-left: 10px;
  font-size: 14px;
}
.inline_search {
  padding: 20px 40px 0;
}
.view_list {
  margin: 20px 0;
}
.fr {
  float: right;
}
.ml20 {
  margin-left: 20px;
}
.pag_content {
  float: right;
  margin: 20px 0;
}
.colorb {
  color: #666 !important;
}
</style>